package org.dromara.crawler.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.logger.FluentLogger;
import org.bson.Document;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.crawler.domain.KnowledgeTemplate;
import org.dromara.crawler.domain.TargetEvent;
import org.dromara.crawler.mongo.mongodomain.ElementEvent;
import org.dromara.crawler.mongo.mongodomain.ElementTarget;
import org.dromara.crawler.service.TargetEventService;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 事件体系接口控制
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/targetEvent")
public class TargetEventController {

    private final TargetEventService targetEventService;


    /**
     * 根据ID获取目标基础信息
     */
    @GetMapping("/getById")
    public R<TargetEvent> getById(@RequestParam Long id) {
        try {
            return R.ok(targetEventService.getById(id));
        } catch (Exception e) {
            log.error("查询失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }


    /**
     * 查询所有事件目表
     */
    @GetMapping("/getAllTargetEvent")
    public R<List<TargetEvent>> getAllTargetEvent() {
        try {
            return R.ok(targetEventService.list());
        } catch (Exception e) {
            log.error("查询失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 添加事件目表
     */
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody TargetEvent targetEvent) {
        try {
            return R.ok(targetEventService.save(targetEvent));
        } catch (Exception e) {
            log.error("添加失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 添加事件目表
     */
    @DeleteMapping("/deleteById")
    public R<Boolean> deleteById(@RequestParam Long id) {
        try {
            return R.ok(targetEventService.deleteTargetEvent(id));
        } catch (Exception e) {
            log.error("删除失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     */
    @GetMapping("/queryByPage")
    public R<IPage<TargetEvent>> queryByPage(TargetEvent targetEvent, PageQuery pageQuery) {
        try {
            return R.ok(targetEventService.queryByPage(targetEvent, pageQuery));
        } catch (Exception e) {
            log.error("查询数据失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }


    /**
     * 删除事件目标
     */
    @DeleteMapping("/deleteTargetEvent")
    public R<String> deleteTargetEvent(Long id) {
        try {
            targetEventService.deleteTargetEvent(id);
            return R.ok("删除成功");
        } catch (Exception e) {
            log.error("删除失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改事件目标
     */
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody TargetEvent targetEvent) {
        try {
            targetEventService.updateById(targetEvent);
            return R.ok("修改成功");
        } catch (Exception e) {
            log.error("修改失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

}
